package cmcshan1;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class VisitSearchView extends JPanel implements ActionListener{
	
	private Driver driver;
	private JTextField searchField;
	private String[] visitSearchOptions = { "Patient ID", "Time", "Date", "Nurse ID", "Doctor ID", "Blood Pressure", "Temperaure", "Pulse", "Symptoms", "Test Ordered", "Test Results", "Diagnosis"};
	private JComboBox searchList;
	private JButton searchButton;
	
	public VisitSearchView(Driver _driver){
		driver = _driver;
		setPreferredSize(new Dimension(1000, 50));
		//setBorder(BorderFactory.createLineBorder(Color.black));
		searchField = new JTextField(40);
		this.add(searchField);
		searchList = new JComboBox(visitSearchOptions);
		this.add(searchList);
		searchButton = new JButton("Search Visits");
		searchButton.addActionListener(this);
		this.add(searchButton);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		ArrayList<String[]> queryTable = new ArrayList<String[]>();
		Connection con = null;
        Statement stmt = null;
        try 
		{   //load JDBC driver
			Class.forName("com.mysql.jdbc.Driver");
		    //connect to the DB
			String connectionString = "jdbc:mysql://cs.westga.edu:3307/cmcshan1?user=cmcshan1&password=cm@3274";
            con = (Connection) DriverManager.getConnection(connectionString);
			stmt = (Statement) con.createStatement();
			
			String columnName = (String)searchList.getSelectedItem();
			if(columnName.equals("Nurse ID"))
				columnName = "NID";
			else if(columnName.equals("Patient ID"))
				columnName ="PID";
			else if(columnName.equals("Doctor ID"))
				columnName ="DID";
			else if(columnName.equals("Blood Pressure"))
				columnName ="bldpressure";
			else if(columnName.equals("Temperature"))
				columnName ="bodytemp";
			else if(columnName.equals("Test Ordered"))
				columnName ="testordered";
			else if(columnName.equals("Test Result"))
					columnName ="testresult";
			String searchParam = searchField.getText();
			//execute SQL statement
			if(columnName.equals("DID"))
			{
				ResultSet rs = stmt.executeQuery("select * from VISITS where " + columnName + " = " + searchParam);
				while (rs.next()) {
					String[] querytuple = {rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12)};
					queryTable.add(querytuple);
				}
			}
			else if(columnName.equals("PID"))
			{
				ResultSet rs = stmt.executeQuery("select * from VISITS where " + columnName + " = " + searchParam);
				while (rs.next()) {
					String[] querytuple = {rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12)};
					queryTable.add(querytuple);
				}
			}
			else if(columnName.equals("NID"))
			{
				ResultSet rs = stmt.executeQuery("select * from VISITS where " + columnName + " = " + searchParam);
				while (rs.next()) {
					String[] querytuple = {rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12)};
					queryTable.add(querytuple);
				}
			}
			else if(columnName.equals("PID"))
			{
				ResultSet rs = stmt.executeQuery("select * from VISITS where " + columnName + " = " + searchParam);
				while (rs.next()) {
					String[] querytuple = {rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12)};
					queryTable.add(querytuple);
				}
			}
			else if(columnName.equals("bldpressure"))
			{
				ResultSet rs = stmt.executeQuery("select * from VISITS where " + columnName + " = " + searchParam);
				while (rs.next()) {
					String[] querytuple = {rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12)};
					queryTable.add(querytuple);
				}
			}
			else if(columnName.equals("bodytemp"))
			{
				ResultSet rs = stmt.executeQuery("select * from VISITS where " + columnName + " = " + searchParam);
				while (rs.next()) {
					String[] querytuple = {rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12)};
					queryTable.add(querytuple);
				}
			}
			else if(columnName.equals("Pulse"))
			{
				ResultSet rs = stmt.executeQuery("select * from VISITS where " + columnName + " = " + searchParam);
				while (rs.next()) {
					String[] querytuple = {rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12)};
					queryTable.add(querytuple);
				}
			}
			else
			{
				ResultSet rs = stmt.executeQuery("select * from VISITS where " + columnName + " = " + "'" + searchParam + "'");
				while (rs.next()) {
					String[] querytuple = {rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12)};
					queryTable.add(querytuple);
				}
			}
            driver.vtView.initializeTable(queryTable);
        }
        catch (Exception exc) 
        {
            System.out.println(exc.toString());
        }
	}

}